﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.SysUser;
using Ekton_Manage_DTO.RequestModels.User;
using Ekton_Manage_Framework.Methods;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using System;
using System.Threading.Tasks;


namespace Ekton_Manage_Business
{

    public class APIUser : IApiSelectUserInfo
    {
        private readonly IBaseRepository<user_list, int> _baseMaterialRepository;
        private readonly IBaseRepository<verify_code, int> _baseverifyCodeRepository;
        public APIUser(IBaseRepository<user_list, int> baseMaterialRepository, IBaseRepository<verify_code, int> baseverifyCodeRepository)
        {
            _baseMaterialRepository = baseMaterialRepository;
            _baseverifyCodeRepository = baseverifyCodeRepository;
        }
        /// <summary>
        /// 修改账户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<Message> ApiPutUserInfo(ApiDtoInsertSysUser request)
        {
            var companys = await _baseMaterialRepository.QuerySingleData(request.id);
            if (companys is null) return new Message("未找到该账户", false, 202);
            companys.name = request.name;
            companys.sex = request.sex;
            companys.phone = request.phone;
            companys.email = request.email;
            var result = await _baseMaterialRepository.UpdateSingleData(companys);
            return new Message(result.Item1);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="prouderType"></param>
        /// <returns></returns>
        public async Task<Message> ApiPutUserPassword(ApiDtoInsertUserPass request)
        {
            var verifyCode = await _baseverifyCodeRepository.QuerySingleData(m => m.email == request.email && m.state == 0);
            if (verifyCode is null)
            {
                return new Message("输入的是无效的邮箱或是无效的验证码，请仔细核对", false, 202);
            }
            if (request.auth_code == verifyCode.code)
            {
                if (DateTime.Now <= verifyCode.create_op_date.AddMinutes(5))
                {
                    var userInfo = await _baseMaterialRepository.QuerySingleData(m => m.account == request.account);
                    userInfo.password = request.password;
                    var result = await _baseMaterialRepository.UpdateSingleData(userInfo);
                    //修改密码完成后修改验证码状态
                    verifyCode.state = 1;
                    var updateCodeState = await _baseverifyCodeRepository.UpdateSingleData(verifyCode);
                    return new Message(result.Item1);
                }
                else
                {
                    //超时后修改验证码状态
                    verifyCode.state = 1;
                    var updateCodeState = await _baseverifyCodeRepository.UpdateSingleData(verifyCode);
                    return new Message("验证码已超时", false, 202);
                }
            }
            else
            {
                return new Message("无效的验证码，请仔细核对", false, 202);
            }
        }


        //查看用户信息
        public async Task<Message> Getaccountn(int id)
        {
            var result = await _baseMaterialRepository.QuerySingleData(id);
            if (result is null) return new Message("未找到该用户", false, 202);
            return new Message(result);
        }

        //查看用户信息
        public async Task<user_list> GetUser(int id)
        {
            return await _baseMaterialRepository.QuerySingleData(m => m.id == id);
        }
    }
}
